package org.muninn.niagara.extensions;

import java.io.IOException;
import java.net.SocketException;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import javax.baja.control.BPointExtension;
import javax.baja.log.Log;
import javax.baja.status.BStatusNumeric;
import javax.baja.status.BStatusString;
import javax.baja.status.BStatusValue;
import javax.baja.sys.Action;
import javax.baja.sys.BAbsTime;
import javax.baja.sys.BIcon;
import javax.baja.sys.Context;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

public class BCloudExt extends BPointExtension
{
  public static final Action sendUpdate = newAction(264);

  public static final Property inPachubeApiKey = newProperty(8, new BStatusString());

  public static final Property inPachubeFeedID = newProperty(8, new BStatusNumeric());

  public static final Property inPachubeFeedDataStream = newProperty(8, new BStatusNumeric(0.0D));

  public static final Property inFeedValue = newProperty(8, new BStatusNumeric(0.0D));

  public static final Property outStatus = newProperty(8, new BStatusString());

  public static final Log logger = Log.getLog("BCloudExt");

  private static final BIcon icon = BIcon.make("local:|module://KorsPachube/com/korsengineering/niagara/pachube/korsLogo.png");
  
  private int c;
  static Class class$com$korsengineering$niagara$pachube$BCloudExt;
  public boolean initiated = initiate();
  
  public boolean initiate(){
	System.out.println("HEyOheyoHEY!!");
	this.makeLink(getParentPoint(), getParentPoint().getSlot("out"), inFeedValue, getPointFacets());
	this.linkTo(getParentPoint(), getParentPoint().getSlot("out"), this.getSlot("inFeedValue"));
	return true;
  }

  public void sendUpdate()
  {
    invoke(sendUpdate, null);
  }

  public void doSendUpdate()
  {
    new SendThread().start();
  }

  String curDes()
  {
    String str1 = "";
    TreeMap localTreeMap = new TreeMap();
    localTreeMap.put("X-PachubeApiKey", getInPachubeApiKey().getValue());
    localTreeMap.put("Content-Type", "application/xml");
    String str2 = "";
//    try {
//      String str3 = "http://api.pachube.com/v2/feeds/" + Integer.toString((int)getInPachubeFeedID().getValue()) + ".xml";
////      str2 = Requester.get(new URL(str3), null, null, localTreeMap);
//
//      int i = str2.indexOf("<description>");
//      if (i > 0)
//      {
//        int j = str2.indexOf("</description>");
//        str1 = str2.substring(i + 13, j);
//      }
//    }
//    catch (SocketException localSocketException) {
//      logger.error(localSocketException.getMessage());
//    }
//    catch (IOException localIOException) {
//      logger.error(localIOException.getMessage());
//    }
    return str1;
  }

  void updateDes(String paramString)
  {
    TreeMap localTreeMap = new TreeMap();
    localTreeMap.put("X-PachubeApiKey", getInPachubeApiKey().getValue());
    localTreeMap.put("Content-Type", "application/xml");

    String str1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<eeml xmlns=\"http://www.eeml.org/xsd/0.5.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"0.5.1\" xsi:schemaLocation=\"http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd\">"
                + "<environment>"
                + "<description>" + paramString + "</description>"
                + "</environment></eeml>";

    String str2 = "";
//    try {
//      String str3 = "http://api.pachube.com/v2/feeds/" + Integer.toString((int)getInPachubeFeedID().getValue()) + ".xml?_method=put";
////      str2 = Requester.post(new URL(str3), str1, null, null, localTreeMap);
//    } catch (SocketException localSocketException) {
//      logger.error(localSocketException.getMessage());
//    }
//    catch (IOException localIOException) {
//      logger.error(localIOException.getMessage());
//    }
  }

  public BStatusString getInPachubeApiKey()
  {
    return (BStatusString)get(inPachubeApiKey); } 
  public void setInPachubeApiKey(BStatusString paramBStatusString) { set(inPachubeApiKey, paramBStatusString); }


  public BStatusNumeric getInPachubeFeedID()
  {
    return (BStatusNumeric)get(inPachubeFeedID); } 
  public void setInPachubeFeedID(BStatusNumeric paramBStatusNumeric) { set(inPachubeFeedID, paramBStatusNumeric); }


  public BStatusNumeric getInPachubeFeedDataStream()
  {
    return (BStatusNumeric)get(inPachubeFeedDataStream); } 
  public void setInPachubeFeedDataStream(BStatusNumeric paramBStatusNumeric) { 
	  set(inPachubeFeedDataStream, paramBStatusNumeric);
  }

  public BStatusNumeric getInFeedValue()
  {
    return (BStatusNumeric)get(inFeedValue); } 
  public void setInFeedValue(BStatusNumeric paramBStatusNumeric) { set(inFeedValue, paramBStatusNumeric); }


  public BStatusString getOutStatus()
  {
    return (BStatusString)get(outStatus); } 
  public void setOutStatus(BStatusString paramBStatusString) { set(outStatus, paramBStatusString); }

  public BIcon getIcon()
  {
    return icon;
  }

  public static final Type TYPE = Sys.loadType(BCloudExt.class);
  public Type getType() { return TYPE; }
  
  // ERROR //
/*  static Class jdMethod_class(String paramString, boolean paramBoolean)
  {
    // Byte code:
    //   0: aload_0
    //   1: invokestatic 232	java/lang/Class:forName	(Ljava/lang/String;)Ljava/lang/Class;
    //   4: iload_1
    //   5: ifne +6 -> 11
    //   8: invokevirtual 236	java/lang/Class:getComponentType	()Ljava/lang/Class;
    //   11: areturn
    //   12: invokevirtual 241	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   15: new 243	java/lang/NoClassDefFoundError
    //   18: dup_x1
    //   19: swap
    //   20: invokespecial 244	java/lang/NoClassDefFoundError:<init>	(Ljava/lang/String;)V
    //   23: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   0	12	12	java/lang/ClassNotFoundException
  }

  private final void jdMethod_this()
  {
    this.c = 0;
  }

  public BCloudExt()
  {
    jdMethod_this();
  }

  static
  {
    Class tmp118_115 = class$com$korsengineering$niagara$pachube$BCloudExt; 
    if (tmp118_115 == null) tmp118_115;
  }
*/
  class SendThread extends Thread
  {
    public void run()
    {
      String str1 = Integer.toString((int)BCloudExt.this.getInPachubeFeedDataStream().getValue()) + ',' + Double.toString((int)BCloudExt.this.getInFeedValue().getValue());
      String str2;
      if ((BCloudExt.this.c == 0) || (BCloudExt.this.c >= 15))
      {
      /*  localObject1 = BCloudExt.this.curDes();
        BCloudExt.logger.trace((String)localObject1 + '|');
        if (((String)localObject1).compareTo("null") == 0) {
          BCloudExt.logger.message("is null");
          localObject1 = "";
        }
        localObject2 = new StringBuffer((String)localObject1);
        str2 = "Feed provided by Kors Engineering free Tridium Pachube Poster.  Contact us at www.korsengineering.com for more information.";
        if (TextUtil.indexOf((StringBuffer)localObject2, str2) < 0)
        {
          BCloudExt.this.updateDes((String)localObject1 + "  " + str2);
        }
        BCloudExt.this.c = 1;*/
      }
      else
      {
        BCloudExt.this.c += 1;
      }

      Object localObject1 = new TreeMap();
      ((Map)localObject1).put("X-PachubeApiKey", BCloudExt.this.getInPachubeApiKey().getValue());
      ((Map)localObject1).put("Content-Type", "text/csv");
      BCloudExt.logger.trace(localObject1.toString());
      BCloudExt.logger.trace(str1);
      Object localObject2 = "";
//      try {
//        str2 = "http://api.pachube.com/v2/feeds/" + Integer.toString((int)BCloudExt.this.getInPachubeFeedID().getValue()) + ".csv?_method=put";
//        localObject2 = Requester.post(new URL(str2), str1, "", "", (Map)localObject1);
//        BCloudExt.logger.trace((String)localObject2);
//        BCloudExt.this.getOutStatus().setValue("Update sent at " + BAbsTime.make().toString());
//      } catch (SocketException localSocketException) {
//        BCloudExt.logger.error(localSocketException.getMessage());
//        BCloudExt.this.getOutStatus().setValue("Error updating: " + localSocketException.getMessage() + " at " + BAbsTime.make().toString());
//      }
//      catch (IOException localIOException) {
//        BCloudExt.this.getOutStatus().setValue("Error updating: " + localIOException.getMessage() + " at " + BAbsTime.make().toString());
//      }
    }

    SendThread()
    {
    }
  }


public void onExecute(BStatusValue arg0, Context arg1) {
	// TODO Auto-generated method stub
	
}

}
















/*package org.muninn.niagara.extensions;

import javax.baja.control.BPointExtension;
import javax.baja.history.ext.BHistoryExt;
import javax.baja.history.ext.BIntervalHistoryExt;
import javax.baja.status.BStatusValue;
import javax.baja.sys.Action;
import javax.baja.sys.BComponent;
import javax.baja.sys.BRelTime;
import javax.baja.sys.Clock;
import javax.baja.sys.Context;
import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

public class BCloudExt extends BPointExtension
{
  public static final Property enable = newProperty(0, true);
  public void setEnable(boolean b) { setBoolean(enable, b); }
  public boolean getEnable() { return getBoolean(enable); }
  
  public static final Property updateReady = newProperty(Flags.READONLY, false);
  public void setUpdateReady(boolean b) { setBoolean(updateReady, b); }
  public boolean getUpdateReady() { return getBoolean(updateReady); }
  
  public static final Property updateFrequency = newProperty(0, BRelTime.make(60000));
  public void setUpdateFrequency(BRelTime t) { set(updateFrequency, t); }
  public BRelTime getUpdateFrequency() { return (BRelTime) get(updateFrequency); }
  
  public static final Property useParentFrequency = newProperty(0, false);
  public void setUseParentFrequency(boolean b) { setBoolean(useParentFrequency, b); }
  public boolean getUseParentFrequency() { return getBoolean(useParentFrequency); }
  
  public static final Action ready = newAction(0, null);
  public void ready() { invoke(ready, null, null); }
  public void doReady() { setUpdateReady(true); }
  
  public void changed (Property p, Context cx)
  {
    if (!isRunning())
      return;
    
    if (p.equals(useParentFrequency) && getUseParentFrequency())
    {
      if (this.getParentComponent() instanceof BIntervalHistoryExt)
        setUpdateFrequency(((BIntervalHistoryExt) this.getParentComponent()).getInterval());
    }
  }
  
  public void stopped() throws Exception { if (ticket != null) ticket.cancel(); }

  Clock.Ticket ticket;
  long lastOnExecuteTicks;
  
  void updateTimer()
  {            
    if (ticket != null) ticket.cancel();
    ticket = Clock.schedulePeriodically(this, getUpdateFrequency(), ready, null);
  }
  
  public void onExecute(BStatusValue arg0, Context arg1) { }
  
  public boolean isParentLegal(BComponent comp) { return comp instanceof BHistoryExt; }
  
  public static final Type TYPE = Sys.loadType(BCloudExt.class);
  public Type getType() { return TYPE; }
}

*/
